﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using CoreEx.Database.Client;
using LinFu.IoC.Interfaces;
using LinFu.IoC;
namespace CoreEx.Database.Metadata.ModelGenerator
{
    class Program
    {
        private static readonly IServiceContainer _serviceContainer = Services.ServiceLocator.Container;
        
        static void Main(string[] args)
        {
            string connectionString = args.Where(a => a.StartsWith(@"/C")).FirstOrDefault();
            string outputDirectory = args.Where(a => a.StartsWith(@"/O")).FirstOrDefault();
            string modelNamespace = args.Where(a => a.StartsWith(@"/N")).FirstOrDefault();
            string providerName  = args.Where(a => a.StartsWith(@"/P")).FirstOrDefault();
            string interfaceOnly = args.Where(a => a.StartsWith(@"/Contract")).FirstOrDefault();
            string implementationOnly = args.Where(a => a.StartsWith(@"/Implementation")).FirstOrDefault();
            
            
           
            
            if (string.IsNullOrEmpty(connectionString) ||
                string.IsNullOrEmpty(outputDirectory) ||
                string.IsNullOrEmpty(modelNamespace) ||
                string.IsNullOrEmpty(providerName))
            {
                ShowUsage();
                return;
            }
            connectionString = connectionString.Substring(3);
            outputDirectory = outputDirectory.Substring(3);
            modelNamespace = modelNamespace.Substring(3);
            providerName = providerName.Substring(3);

            _serviceContainer.GetService<IConnectionInfoInjector>().Inject(modelNamespace,providerName,connectionString);
                                
            var generator = _serviceContainer.GetService<IModelGenerator>();
            generator.Namespace = modelNamespace;
            generator.OutputDirectory = outputDirectory;
            if (!string.IsNullOrEmpty(interfaceOnly))
                generator.Generate(modelNamespace,ModelGenerationFlags.Contract);
            else
                if (string.IsNullOrEmpty(interfaceOnly) && string.IsNullOrEmpty(implementationOnly))
                    generator.Generate(modelNamespace, ModelGenerationFlags.Contract & ModelGenerationFlags.Implementation);

            if (!string.IsNullOrEmpty(implementationOnly))
                generator.Generate(modelNamespace, ModelGenerationFlags.Implementation);
            else
                if (!string.IsNullOrEmpty(interfaceOnly) && !string.IsNullOrEmpty(implementationOnly))
                    generator.Generate(modelNamespace, ModelGenerationFlags.Contract & ModelGenerationFlags.Implementation);
            
        }


        private static void ShowUsage()
        {
            Console.WriteLine(@"ModelGenerator.exe /C=[ConnectionString] /P=[ProviderName] /O=[OutputDirectory] /N=[Namespace]");
            Console.WriteLine("Press any key to continue");
            Console.ReadLine();
        }
    }
}
